package com.bmps.springcloud.insuredmicroservice.service.calculate;

import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.RiskConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.medium.PolicyRiskInfoModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.riskamount.InsuranceRiskAmountStrategy;
import com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.riskamount.InsuranceRiskStrategyFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * description:
 *
 * @author jackdaw
 * @date 2020-04-15 16:05
 */
@Service
@Slf4j
public class InsuranceRiskAmountServiceImpl implements InsuranceRiskAmountService {
    private final RedisService redisService;

    public InsuranceRiskAmountServiceImpl(RedisService redisService) {
        this.redisService = redisService;
    }


    @Override
    public BigDecimal sumInsuranceRiskAmount(String riskSort, List<PolicyRiskInfoModel> riskInfoList, boolean isCurrent) {
        //查询缓存中寿险配置
        Set riskCodeSet;
        try {
            riskCodeSet = redisService.members(riskSort);
        } catch (Exception e) {
            log.error("InsuranceRiskAmountServiceImpl.sumLifeInsuranceAmount查询缓存key=" + riskSort + "异常");
            e.printStackTrace();
            return BigDecimal.valueOf(-1);
        }
        if (CollectionUtils.isEmpty(riskCodeSet)) {
            log.error("InsuranceRiskAmountServiceImpl.sumLifeInsuranceAmount查询缓存key=" + riskSort + "无配置信息");
            return BigDecimal.valueOf(-1);
        }
        BigDecimal sum = BigDecimal.valueOf(0);
        for (PolicyRiskInfoModel riskInfoModel : riskInfoList) {
            if (riskCodeSet.contains(riskInfoModel.getRiskCode())) {
                sum = sum.add(riskInfoModel.getAmnt());
            }
        }
//        return getInsuranceRiskAmount(riskCodeSet, riskInfoList, isCurrent);
        return sum;
    }

    @Override
    public BigDecimal sumAat053InsuranceAmount(List<PolicyRiskInfoModel> riskInfoList) {
        BigDecimal sum = BigDecimal.valueOf(0);
        if (CollectionUtils.isNotEmpty(riskInfoList)) {
            for (PolicyRiskInfoModel policyRiskInfoModel : riskInfoList) {
                if (StringUtils.equals(RiskConstant.AAT053, policyRiskInfoModel.getRiskCode())) {
                    if (policyRiskInfoModel.getAmnt().compareTo(BigDecimal.valueOf(0)) < 0) {
                        return BigDecimal.valueOf(-1);
                    }
                    sum = sum.add(policyRiskInfoModel.getAmnt());
                }
            }

        }
        return sum;
    }

    @Override
    public BigDecimal sumExemptDutyAccidentInsuranceAmount(List<PolicyRiskInfoModel> riskInfoList, boolean isCurrent) {
        BigDecimal sum = BigDecimal.valueOf(0);
        if (CollectionUtils.isNotEmpty(riskInfoList)) {
            for (PolicyRiskInfoModel policyRiskInfoModel : riskInfoList) {
                Object object = redisService.get(AcceptConstant.EXEMPT_DUTY_RISK + policyRiskInfoModel.getRiskCode());
                if (object != null) {
                    InsuranceRiskAmountStrategy strategy = InsuranceRiskStrategyFactory.getStrategy(policyRiskInfoModel.getRiskCode());
                    sum = sum.add(strategy.getRiskAmount(policyRiskInfoModel, isCurrent, object));
                }
            }
        }
        return sum;
    }

    /**
     * 获取风险保额
     *
     * @param riskSort  风险保额类型
     * @param models    保单集合
     * @param isCurrent true-当前单
     * @return 风险保额
     */
    private BigDecimal getInsuranceRiskAmount(Set riskSort, List<PolicyRiskInfoModel> models, boolean isCurrent) {
        BigDecimal sum = BigDecimal.valueOf(0);
        if (CollectionUtils.isNotEmpty(models)) {
            for (PolicyRiskInfoModel policyRiskInfoModel : models) {
                //判断险种是否累积风险保额
                if (riskSort.contains(policyRiskInfoModel.getRiskCode())) {
                    InsuranceRiskAmountStrategy strategy = InsuranceRiskStrategyFactory.getStrategy(policyRiskInfoModel.getRiskCode());
                    sum = sum.add(strategy == null ?
                            policyRiskInfoModel.getAmnt() : strategy.getRiskAmount(policyRiskInfoModel, isCurrent, null));
                }
            }
        }
        return sum;
    }
}
